using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace XFNLink
{
    public partial class InsertUC : UserControl
    {
        private string link;

        public string Link
        {
            get { return link; }
            set { link = value; }
        }

        public InsertUC()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            string html = "<a href=\"";

            if ((!string.IsNullOrEmpty(txtText.Text)) &&
                (!string.IsNullOrEmpty(txtUrl.Text)))
            {
                html += txtUrl.Text + "\"";

                if (!string.IsNullOrEmpty(txtTitle.Text))
                {
                    html += " title=\"" + txtTitle.Text + "\"";
                }

                if (chkNewWin.Checked)
                {
                    html += " target=\"_new\"";
                }

                List<string> relations = GetRelations();
                if ((relations.Count > 0) ||
                    (!string.IsNullOrEmpty(cbRel.Text)))
                {
                    html += " rel=\"";

                    if (!string.IsNullOrEmpty(cbRel.Text))
                        html += cbRel.Text;

                    if (relations.Count > 0)
                    {
                        string relationsText = string.Join(" ",
                            (string[])relations.ConvertAll
                            (new Converter<string, string>(delegate(string value)
                            {
                                return Convert.ToString(value);
                            }
                            )).ToArray());

                        if (html.EndsWith("\""))
                            html += relationsText;
                        else
                            html += " " + relationsText;
                    }

                    html += "\"";
                }

                html += ">" + txtText.Text + "</a>";

                this.link = html;
            }
        }

        private List<string> GetRelations()
        {
            List<string> relations = new List<string>();
            foreach (Control control in gbRelations.Controls)
            {
                CheckBox chkRelation = control as CheckBox;
                if (chkRelation.Checked)
                {
                    relations.Add(chkRelation.Text);
                }
            }
            return relations;
        }
    }
}
